package managedBeans;

import java.util.Date;
import java.util.Vector;

import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.html.HtmlDataTable;
import javax.faces.context.FacesContext;
import javax.faces.component.html.HtmlDataTable;




import model.Kunden;
import model.Reservierung;
import model.Zimmer;



import ejbs.TransactionsBean;

@SessionScoped
@ManagedBean
public class KundeManagement {

	@EJB
	private TransactionsBean transactionsBean;
	
    
	private String name ="", rechnungAdrresse="", firmenname ="", notiz ="", tel ="", email ="", web ="", fax ="";
	private float rabatt = 0;
	private String errorMsg ;
	private HtmlDataTable dtKunden;
	private Vector<Kunden> kunden = new Vector<Kunden>();
	private boolean kundenFound =false;
	private String  kundeNameSearch ;
	private int kundeRecordId ;
	private Date reservierungVon, reservierungBis;
	
	  
	
	// 11.12.2011, get the guest data and save it in the database
	public String addKunde(){
		FacesContext ctx = FacesContext.getCurrentInstance();
		
		Kunden k = new Kunden();
		k.setName(this.name);
		k.setRechnungAdrresse(this.rechnungAdrresse);
		k.setFirmenname(this.firmenname);
		k.setNotiz(this.notiz);
		k.setTel(this.tel);
		k.setEmail(this.email);
		k.setWeb(this.web);
		k.setFax(this.fax);
		k.setRabatt(this.rabatt);
		 
		   
		if(transactionsBean.saveNewKunden(k)){
			
			 ctx.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Der Kunde wurde erfolgreich gespeichert" , "Success"));
			 clearFields();
			
		}else{
			 errorMsg = transactionsBean.getErr();
			 ctx.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, transactionsBean.getErr() , "Error"));
			 
			
		}	
		
		// return ("kundehinzufuegen");
		 return ("reservierung");
	}

	// 11.12.2011, get the guest data and save it in the database
	public String editKunde(){
		FacesContext ctx = FacesContext.getCurrentInstance();
		Kunden kunde = transactionsBean.getKunde(this.kundeRecordId);
		kunde.setName(this.firmenname);
		kunde.setRechnungAdrresse(this.rechnungAdrresse);
		kunde.setFirmenname(this.firmenname);
		kunde.setNotiz(this.notiz);
		kunde.setTel(this.tel);
		kunde.setEmail(this.email);
		kunde.setFax(this.fax);
		kunde.setRabatt(this.rabatt); 
		
		if(transactionsBean.editGuest(kunde)){
			ctx.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Die Daten wurden erfolgreich gespeichert","Success"));
		}else{
			ctx.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, transactionsBean.getErr() , "Error"));
		}
		
		return ("kundenbearbeiten"); 
	}
	// 11.12.2011, go to the user details
	public String showGuestDetails(){
		FacesContext ctx = FacesContext.getCurrentInstance();
		
		Kunden kunde = (Kunden) dtKunden.getRowData();
		
		this.kundeRecordId = kunde.getId();
		this.name = kunde.getName();
		this.rechnungAdrresse = kunde.getRechnungAdrresse();
		this.firmenname = kunde.getFirmenname();
		this.notiz = kunde.getNotiz();
		this.tel = kunde.getTel();
		this.email = kunde.getEmail();
		this.web = kunde.getWeb();
		this.fax = kunde.getFax();
		this.rabatt = kunde.getRabatt();
		
		  
		ctx.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "The guest Id: "+ kunde.getId(), "Success"));
		 
				 
		return "kundenbearbeiten";
	}
	  
	// 11.12.2011...Search guests by name
	public String searchKundeByName(){
		
		FacesContext ctx = FacesContext.getCurrentInstance();
		kunden.clear();
		dtKunden.clearInitialState();
		
		
		
		kunden = transactionsBean.searchKunden(this.kundeNameSearch);
		
		if(kunden.size() > 0){
			this.kundenFound = true;
			ctx.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "es wurden "+ kunden.size() + " Kunden gefunden!","Success"));
		}
		else{ 
			this.kundenFound = false;
			ctx.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Es wurden keinen Kunden gefunden" , "Error"));
		}
		
		
		
		return "kundeverwalten";
	}
	
	
	
	
	// 11.12.2011 
	//The method is used to clear the fields after the successful save
	
	
	// 12.12.2011
	// Save the reservation
	public String reserve(){
		  
		
		Kunden kunde = new Kunden();
		kunde.setName(this.name);
		kunde.setFirmenname(this.firmenname);
		kunde.setRechnungAdrresse(this.rechnungAdrresse);
		kunde.setNotiz(this.notiz);
		kunde.setEmail(this.email);
		
		Zimmer zimmer = new Zimmer();
		zimmer.setName("TestZimmer");
		
		Reservierung reservierung = new Reservierung();
		reservierung.setReservierungVon(reservierungVon);
		reservierung.setReservierungBis(reservierungBis);
		
		transactionsBean.saveReservation(kunde, reservierung, zimmer);
		
		
		
		return "reservierung";
	}
	
		private void clearFields(){
		this.name  ="";
		this.rechnungAdrresse  ="";
		this.firmenname  ="";
		this.notiz  ="";
		this.tel  ="";
		this.email  ="";
		this.web  ="";
		
		this.fax  ="";
		this.rabatt  =0;
	}
	

	public TransactionsBean getTransactionsBean() {
		return transactionsBean;
	}


	public void setTransactionsBean(TransactionsBean transactionsBean) {
		this.transactionsBean = transactionsBean;
	}


	public String getName() {
		return name;
	}


	public void setName(String name) {
		this.name = name;
	}


	public String getRechnungAdrresse() {
		return rechnungAdrresse;
	}


	public void setRechnungAdrresse(String rechnungAdrresse) {
		this.rechnungAdrresse = rechnungAdrresse;
	}


	public String getFirmenname() {
		return firmenname;
	}


	public void setFirmenname(String firmenname) {
		this.firmenname = firmenname;
	}


	public String getNotiz() {
		return notiz;
	}


	public void setNotiz(String notiz) {
		this.notiz = notiz;
	}


	

	public String getTel() {
		return tel;
	}


	public void setTel(String tel) {
		this.tel = tel;
	}




	public String getEmail() {
		return email;
	}


	public void setEmail(String email) {
		this.email = email;
	}


	public String getErrorMsg() {
		return errorMsg;
	}


	public void setErrorMsg(String errorMsg) {
		this.errorMsg = errorMsg;
	}


	public String getWeb() {
		return web;
	}


	public void setWeb(String web) {
		this.web = web;
	}


	public String getFax() {
		return fax;
	}


	public void setFax(String fax) {
		this.fax = fax;
	}


	public float getRabatt() {
		return rabatt;
	}


	public void setRabatt(float rabatt) {
		this.rabatt = rabatt;
	}


	public HtmlDataTable getDtKunden() {
		return dtKunden;
	}


	public void setDtKunden(HtmlDataTable dtKunden) {
		this.dtKunden = dtKunden;
	}


	public Vector<Kunden> getKunden() {
		return kunden;
	}


	public void setKunden(Vector<Kunden> kunden) {
		this.kunden = kunden;
	}


	public boolean isKundenFound() {
		return kundenFound;
	}


	public void setKundenFound(boolean kundenFound) {
		this.kundenFound = kundenFound;
	}


	public String getKundeNameSearch() {
		return kundeNameSearch;
	}


	public void setKundeNameSearch(String kundeNameSearch) {
		this.kundeNameSearch = kundeNameSearch;
	}

	public int getKundeRecordId() {
		return kundeRecordId;
	}

	public void setKundeRecordId(int kundeRecordId) {
		this.kundeRecordId = kundeRecordId;
	}

	public Date getReservierungVon() {
		return reservierungVon;
	}

	public void setReservierungVon(Date reservierungVon) {
		this.reservierungVon = reservierungVon;
	}

	public Date getReservierungBis() {
		return reservierungBis;
	}

	public void setReservierungBis(Date reservierungBis) {
		this.reservierungBis = reservierungBis;
	}

	
}
